﻿using System;
using System.IO;
using System.Web.Configuration;
using System.Web.UI.WebControls;
using eCommerce.core;
using eCommerce.core.AdminMenu;
using eCommerce.core.Authenticated;
using eCommerce.core.Helpers;

namespace eCommerce.cms
{
    public partial class Default : AuthenticatedPage
    {
        protected int AdminMenuId;
        protected string Domain;
        protected string AdminUserId;
        protected string ActiveMenu;

        protected void Page_Load(object sender, EventArgs e)
        {
            AdminMenuId = Utils.GetIdFromRequestString(Utils.ControlId, 0);

            Page.Title  = Utils.GetSetting("SiteName", SettingType.String).ToString();
            ltBread.Text = BuildBread();
        }

        private string BuildBread()
        {
            var bread = string.Empty;
            var cId = Utils.GetIdFromRequestString(Utils.ControlId, 0);
            if (cId > 0)
            {
                var m = AdminMenuController.AdminMenu_GetById(cId);
                if (m != null)
                {
                    AdminMenuObject pm = null;
                    try { pm = AdminMenuController.AdminMenu_GetById(m.ParentId); }
                    catch { }

                    bread += "<div class=\"title\"><h5>";

                    

                    if (pm != null)
                        bread += pm.Name + " / " ;

                    bread += m.Name;

                    bread += "</h5></div>";

                    ActiveMenu = m.CtrlKey;
                }
            }
            return bread;
        }

        protected override void OnInit(EventArgs e)
        {
            var controlId = Utils.GetIdFromRequestString(Utils.ControlId, 0);
            if (controlId != 0)
            {
                var lst = AdminMenuController.AdminMenu_GetAllByStatusAndUserId(
                                            AdminMenuObject.IsVisibled + AdminMenuObject.IsVisibledWhenRun,
                                            CurrentUser.AdminUserId);

                // check if user control file exist ?
                var menuItem = new AdminMenuObject();
                foreach (var o in lst)
                {
                    if (o.AdminMenuId != controlId) continue;
                    menuItem = o;
                    break;
                }

                if (!string.IsNullOrEmpty(menuItem.CtrlSource))
                {
                    if (!File.Exists(Server.MapPath(menuItem.CtrlSource)))
                    {
                        var ltMessage = new Literal
                        {
                            Text = Utils.ShowMessage(string.Format("{0} - Chức năng này đang xây dựng. Vui lòng quay lại sau : )", menuItem.CtrlSource),
                                    MessageType.Information)
                        };

                        pHolder.Controls.Add(ltMessage);
                        return;
                    }

                    var curControl = LoadControl(menuItem.CtrlSource);
                    if (curControl != null)
                        pHolder.Controls.Add(curControl);
                }
                else
                {
                    pHolder.Controls.Add(LoadControl("/Controls/AccessDenied.ascx"));
                    return;
                }
            }
            else
                Response.Redirect(WebConfigurationManager.AppSettings["DefaultControl"]);
        }
    }
}